// pagesEnterprise/pages/parkDetails/parkDetails.js
import ajax from "../../../utils/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gardenId: '',
    tabActive: 1,
    canvasShow: false,

    /**
     * 轮播图下标
     */
    swiperIndex: 0,
    /**
     * 图片对象
     */
    gardenImage: [],
    /**
     * 房产装修情况
     */
    fitmentCaseArr: ['精装修', '简装', '毛坯'],
    /**
     * 地图标签
     */
    markers: [],
    /**
     * 详情对象
     */
    entity: null,
    /**
     * 房源查询对象
     */
    queryData: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    },
    /**
     * 房源数据列表
     */
    housingList: [],
    enterpriseList: [], // 企业列表
    /**
     * 分享弹出框
     */
    showShare: false,
    /**
     * 查看全部介绍
     */
    lookAllDesc: false,
    userInfo: null,
    showLoginPicker: false,
  },
  loginSuccess() {
    this.loginCancel()
    this.setData({ userInfo: wx.getStorageSync('userInfo') || null })
    this.getParkDetail()
  },
  loginCancel() {
    this.setData({ showLoginPicker: false })
  },
  isModeEvent() {
    this.setData({ showLoginPicker: true })
  },
  lookAllEventr() {
    this.setData({ lookAllDesc: !this.data.lookAllDesc })
  },
  swiperCutEvent(event) {
    this.setData({ swiperIndex: parseInt(event.currentTarget.dataset.type) })
  },
  swiperChange(event) {
    console.log('swiperChange', event)
    this.setData({ swiperIndex: event.detail.current - 0 })
  },
  navigatorEvent(event) {
    const url = event.currentTarget.dataset.url
    wx.navigateTo({ url })
  },
  async followEvent() {
    const { gardenId, entity } = this.data
    await ajax.post('/follow/add', {
      followType: 2,
      gardenId,
      state: entity.follow ? 0 : 1
    })
    const follow = entity.follow ? false : true
    ajax.showToast(follow ? '关注成功' : '取消关注成功')
    this.setData({ 'entity.follow': follow })
  },
  /**
   * 拨打电话
   */
  callPhoneEvent(event) {
    const phone = event.currentTarget.dataset.phone
    if (!phone) return ajax.showToast('未获得手机号')
    ajax.makePhoneCall(phone)
  },
  tabEvent(event) {
    const tabActive = event.currentTarget.dataset.index
    this.setData({ tabActive })
    this.data.queryData.pageNum = 1
    tabActive == 1 ? this.getGardenEnterprise() : this.getGardenHousingList()
  },
  /**
   * 显示/隐藏分享框
   */
  shareEvent(event) {
    this.setData({ showShare: !(event.detail == 1) })
  },
  /**
   * 轮播图变化事件
   */
  swiperChange(event) {
    this.setData({ swiperIndex: event.detail.current })
  },
  /**
   * 地图点击事件
   */
  mapEvent() {
    const entity = this.data.entity
    ajax.openMmap({
      lat: entity.wxGarden.latitude || null,
      lng: entity.wxGarden.longitude || null,
      address: `${entity.wxGarden.province || ''}${entity.wxGarden.city || ''}${entity.wxGarden.county || ''}${entity.wxGarden.address || ''}${entity.wxGarden.gardenName || ''}`
    }, 2)
  },

  /**
   * 获取园区详情
   */
  async getParkDetail() {
    const { gardenId, userInfo } = this.data
    const res = await ajax.get(`/garden/gardenHome?id=${gardenId}&wxUserId=${userInfo?.id || ''}`)
    this.setData({ entity: res.data })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.shareId = options.shareId || null
    if (options.scene) {
      const entity = ajax.windowSearch(options.scene)
      options.id = entity.id
      app.globalData.shareId = entity.shareId || null
    }
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    this.setData({ gardenId: options.id, userInfo: wx.getStorageSync('userInfo') || null })
    console.log('userInfo', this.data.userInfo)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ userInfo: wx.getStorageSync('userInfo') || null })
    this.getParkDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
     * 朋友圈分享
     */
  onShareTimeline: function () {
    const { entity } = this.data
    const userInfo = wx.getStorageSync('userInfo')
    return {
      title: `${userInfo.nickName}邀请您预览${entity.wxGarden.gardenName}`,
      query: `id=${entity.wxGarden.id}&shareId=${userInfo.id}`,
      imageUrl: entity.wxGarden.indexImage,
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const { entity } = this.data
    const userInfo = wx.getStorageSync('userInfo')
    this.shareEvent({ detail: 1 })
    return {
      title: `${userInfo.nickName}邀请您预览${entity.wxGarden.gardenName}`,
      imageUrl: entity.wxGarden.indexImage,
      path: `/pagesEnterprise/pages/parkDetails/parkDetails?id=${entity.wxGarden.id}&shareId=${userInfo.id}`
    }
  }
})